package com.qst.rongxiaoserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.rongxiaoserver.common.Result;
import com.qst.rongxiaoserver.model.KnowledgeModel;
import com.qst.rongxiaoserver.service.IKnowledgeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/knowledge")
@Tag(name = "知识管理", description = "知识管理相关接口")
public class KnowledgeController {

    @Autowired
    IKnowledgeService knowledgeService;

    // 添加知识
    @PostMapping("/add")
    @Operation(summary = "添加知识", description = "添加知识")
    public Result<KnowledgeModel> addKnowledge(@Valid @RequestBody KnowledgeModel knowledgeModel) {
       boolean success =   knowledgeService.save(knowledgeModel);
       return success? Result.success(knowledgeModel,"添加成功"):Result.fail("添加失败");
    }

    // 修改知识
    @PostMapping("/update")
    @Operation(summary = "编辑知识信息", description = "编辑知识")
    public Result<KnowledgeModel> updateKnowledge(@Valid @RequestBody KnowledgeModel knowledgeModel) {
        boolean success =   knowledgeService.updateById(knowledgeModel);
        return success? Result.success(knowledgeModel,"编辑成功"):Result.fail("编辑失败");
    }

    // 查询知识
    @GetMapping("/list")
    @Operation(summary = "知识列表", description = "知识列表")
    public Result<Page<KnowledgeModel>> getKnowledges(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "title", required = false) String title) {

       Page<KnowledgeModel> knowledgeList =  knowledgeService.getKnowledgeList(pageNum, pageSize, title);
        return knowledgeList!=null ? Result.success(knowledgeList,"查询成功"):Result.fail("查询失败");
    }

    // 删除知识
    @DeleteMapping("/delete")
    @Operation(summary = "根据ID删除知识", description = "根据ID删除知识")
    public Result deleteKnowledge(@Valid @RequestParam Integer knowledgeId) {
        boolean success =   knowledgeService.removeById(knowledgeId);
        return success? Result.success("删除成功"):Result.fail("删除失败");
    }

  // 获取知识详情
    @GetMapping("/detail")
    @Operation(summary = "根据ID获取知识详情", description = "根据ID获取知识详情")
    public Result<KnowledgeModel> detailKnowledge(@Valid @RequestParam Integer knowledgeId) {
        KnowledgeModel knowledgeModel =  knowledgeService.getById(knowledgeId);
        return knowledgeModel!=null? Result.success(knowledgeModel,"查询成功"):Result.fail("查询失败");
    }
}